/*
 * SDL_video.c
 *
 *  Created on: Nov 29, 2020
 *      Author: ZKSWE Develop Team
 */
#include "main.h"
#include "video.h"


#define CLR_CS()	(GPIOA->BRE = GPIO_Pins_12)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET)
#define SET_CS()	(GPIOC->BSRE = GPIO_Pins_12)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET)

// RD
//#define CLR_RD()	(GPIOC->DATAPIN5 = 0)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,GPIO_PIN_RESET)
#define SET_RD() //	(GPIOC->DATAPIN5 = 1)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,GPIO_PIN_SET)

//0x50000400UL
// WR PB13
#define CLR_CLK()	(GPIOA->BRE = GPIO_Pins_15)//(*(unsigned int *)0x50000428UL = GPIO_PIN_13)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,GPIO_PIN_RESET)
#define SET_CLK()	(GPIOA->BSRE = GPIO_Pins_15)///(*(unsigned int *)0x50000418UL = GPIO_PIN_13)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,GPIO_PIN_SET)

//RS
//PB14
#define SET_CMD()	(GPIOA->BRE = GPIO_Pins_8)//(*(unsigned int *)0x50000428UL = GPIO_PIN_14)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_RESET)
#define SET_DATA()	(GPIOA->BSRE = GPIO_Pins_8)//(*(unsigned int *)0x50000418UL = GPIO_PIN_14)//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,GPIO_PIN_SET)

#define RST_LOW()	(GPIOF->BRE = GPIO_Pins_6)//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET)
#define RST_HI()	(GPIOF->BSRE = GPIO_Pins_6)//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET)

#define LED_ON() 	(GPIOA->BRE = GPIO_Pins_11) //HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_RESET)
#define LED_OFF() 	(GPIOA->BSRE = GPIO_Pins_11) //HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,GPIO_PIN_SET)


#define TFT_SPI_Write_Byte(num)		{GPIOB->OPTDT = num; CLR_CLK(); SET_CLK(); }

void LCD_startWriteMutileData(){
	SET_DATA();
	CLR_CS();
	SET_CLK();
	//pbTemp = GPIOB->ODR&0xff00;
}
void LCD_WritemutileData(uint8_t data){
	TFT_SPI_Write_Byte(data);
}

void LCD_endWriteMutileData(){
	SET_CS();
}

void LCD_WR_REG(uint8_t CMD)
{
	CLR_CS();
	SET_CMD();
	SET_RD();
	TFT_SPI_Write_Byte(CMD);
	SET_CS();
}

void LCD_WR_DATA(uint8_t DATA)
{
	CLR_CS();
	SET_DATA();
	TFT_SPI_Write_Byte(DATA);
	SET_CS();
}
void LCD_WR_DATA16(uint16_t DATA)
{
	SET_DATA();
	CLR_CS();

	//TFT_SPI_Write_16(DATA);
	TFT_SPI_Write_Byte(DATA>>8);
	TFT_SPI_Write_Byte(DATA&0xff);
	SET_CS();
}
#define LCD_ILI9341_CMD 		LCD_WR_REG
#define LCD_ILI9341_Parameter 	LCD_WR_DATA
#define delay_ms 				HAL_Delay
#define Delay			HAL_Delay
#define WriteData	LCD_WR_DATA
#define WriteComm LCD_WR_REG

#define LCD_DIR 0	//����
#define LCD_width 	480
#define LCD_height	320
#define LCD_wramcmd 0X2C
#define LCD_setxcmd 0X2A
#define LCD_setycmd 0X2B
void LCD_Clear(uint16_t color)
{
	LCD_Fill(0,0,LCD_width,LCD_height,color);
}  

bool video_init() {
	
  LED_OFF();
	RST_LOW();
	delay_ms(100);
	RST_HI();
	delay_ms(120);
	SET_RD();

LCD_WR_REG(0xE0); 
LCD_WR_DATA(0x00); 
LCD_WR_DATA(0x04); 
LCD_WR_DATA(0x0E); 
LCD_WR_DATA(0x08); 
LCD_WR_DATA(0x17); 
LCD_WR_DATA(0x0A); 
LCD_WR_DATA(0x40); 
LCD_WR_DATA(0x79); 
LCD_WR_DATA(0x4D); 
LCD_WR_DATA(0x07); 
LCD_WR_DATA(0x0E); 
LCD_WR_DATA(0x0A); 
LCD_WR_DATA(0x1A); 
LCD_WR_DATA(0x1D); 
LCD_WR_DATA(0x0F);  
 
LCD_WR_REG(0xE1); 
LCD_WR_DATA(0x00); 
LCD_WR_DATA(0x1B); 
LCD_WR_DATA(0x1F); 
LCD_WR_DATA(0x02); 
LCD_WR_DATA(0x10); 
LCD_WR_DATA(0x05); 
LCD_WR_DATA(0x32); 
LCD_WR_DATA(0x34); 
LCD_WR_DATA(0x43); 
LCD_WR_DATA(0x02); 
LCD_WR_DATA(0x0A); 
LCD_WR_DATA(0x09); 
LCD_WR_DATA(0x33); 
LCD_WR_DATA(0x37); 
LCD_WR_DATA(0x0F); 

LCD_WR_REG(0xC0); 
LCD_WR_DATA(0x18); 
LCD_WR_DATA(0x16); 
 
LCD_WR_REG(0xC1); 
LCD_WR_DATA(0x41); 

LCD_WR_REG(0xC5); 
LCD_WR_DATA(0x00); 
LCD_WR_DATA(0x28); //22
LCD_WR_DATA(0x80); 

LCD_WR_REG(0x36); 
LCD_WR_DATA(0xA8); 

LCD_WR_REG(0x3A); //Interface Mode Control
LCD_WR_DATA(0x55);


LCD_WR_REG(0XB0);  //Interface Mode Control  
LCD_WR_DATA(0x00); 
LCD_WR_REG(0xB1);   //Frame rate 70HZ  
LCD_WR_DATA(0xB0); 

LCD_WR_REG(0xB4); 
LCD_WR_DATA(0x02);   

LCD_WR_REG(0xB6); //RGB/MCU Interface Control
LCD_WR_DATA(0x02); 
LCD_WR_DATA(0x22); 

LCD_WR_REG(0xE9); 
LCD_WR_DATA(0x00);
 
LCD_WR_REG(0XF7);    
LCD_WR_DATA(0xA9); 
LCD_WR_DATA(0x51); 
LCD_WR_DATA(0x2C); 
LCD_WR_DATA(0x82);

LCD_WR_REG(0x11); 
delay_ms(120); 
LCD_WR_REG(0x29); 
	 delay_ms(10);
	 LCD_Clear(0x0);
	 
  LED_ON();
	return true;
}

void video_deinit() {
}

void video_flip() {
	
}